package com.ruoyi.operation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.operation.domain.TFileList;
import com.ruoyi.operation.domain.TFileResult;
import com.ruoyi.operation.domain.bo.TFileListBo;
import com.ruoyi.operation.domain.vo.TFileListVo;
import com.ruoyi.operation.mapper.TFileListMapper;
import com.ruoyi.operation.service.ITFileListService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 文件列Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-22
 */
@RequiredArgsConstructor
@Service
public class TFileListServiceImpl implements ITFileListService {

    private final TFileListMapper baseMapper;

    /**
     * 查询文件列
     */
    @Override
    public TFileListVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询文件列列表
     */
    @Override
    public TableDataInfo<TFileListVo> queryPageList(TFileListBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TFileList> lqw = buildQueryWrapper(bo);
        Page<TFileListVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询文件列列表
     */
    @Override
    public List<TFileListVo> queryList(TFileListBo bo) {
        LambdaQueryWrapper<TFileList> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TFileList> buildQueryWrapper(TFileListBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TFileList> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getFilegroupid()), TFileList::getFilegroupid, bo.getFilegroupid());
        lqw.eq(StringUtils.isNotBlank(bo.getChilepath()), TFileList::getChilepath, bo.getChilepath());
        lqw.like(StringUtils.isNotBlank(bo.getFilename()), TFileList::getFilename, bo.getFilename());
        lqw.eq(StringUtils.isNotBlank(bo.getMemo()), TFileList::getMemo, bo.getMemo());
        lqw.eq(StringUtils.isNotBlank(bo.getPeriod()), TFileList::getPeriod, bo.getPeriod());
        lqw.eq(bo.getStarttime() != null, TFileList::getStarttime, bo.getStarttime());
        lqw.eq(StringUtils.isNotBlank(bo.getFlag()), TFileList::getFlag, bo.getFlag());
        return lqw;
    }

    /**
     * 新增文件列
     */
    @Override
    public Boolean insertByBo(TFileListBo bo) {
        TFileList add = BeanUtil.toBean(bo, TFileList.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改文件列
     */
    @Override
    public Boolean updateByBo(TFileListBo bo) {
        TFileList update = BeanUtil.toBean(bo, TFileList.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TFileList entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除文件列
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

}
